<?php

use App\Models\Cruise\CruiseProjectCategoryModel;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Jialeo\LaravelSchemaExtend\Schema;

class AddTypeToCruiseCategoryTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('cruise_project_category', function (Blueprint $table) {
            $table->unsignedSmallInteger('type')->default(0)->comment('cruise_template.type')->after('sort');
        });

        // 历史数据处理
        \App\Models\Cruise\CruiseProjectCategoryModel::query()->where('pid', 0)->get()->each(function ($item) {

            switch ($item->name) {
                case "新店帮扶":
                    $type = \App\Models\Cruise\CruiseTemplateModel::TYPE_1;
                    break;

                case "社区学校店":
                case "社区街边店":
                case "校内食堂店":
                case "学校商街店":
                case "商场金街店":
                case "景区内部店":
                case "商场内部店":
                    $type = \App\Models\Cruise\CruiseTemplateModel::TYPE_2;
                    break;

                case "品控~巡检":
                case "品控~品牌形象检查":
                    $type = \App\Models\Cruise\CruiseTemplateModel::TYPE_3;
                    break;
                default:
                    $type = 0;
                    break;
            }

            $item->type = $type;
            $item->save();

            CruiseProjectCategoryModel::query()->where('pid', $item->id)->update(['type' => $type]);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('cruise_project_category', function (Blueprint $table) {
            $table->dropColumn('type');
        });
    }
}
